<?php

namespace app\admin\controller;
use think\Controller;


class User extends Auth
{

    //会员账号管理
    //*+------------------------------------------------------------------------+*/

    private  $sort_ary=['time_login'=>'登录时间','time_reg'=>'注册时间','count_login'=>'登录次数','cash'=>'账号余额']; //,'signcount'=>'签到'
    private  $sort_atr=['desc'=>'降序','asc'=>'升序'];


    //账号列表
    public function index(){
        $id=input('id');
        if($id)$map[]=['id','like','%'.$id.'%'];

    	//按状态查询
        $state=input('state',0);
    	if($state)$map[]=['state','=',$state];

        //按关键词查询
        $keyword=input('keyword','','trim');
        if($keyword)$map[] = ['account|mobile|email','like','%'.$keyword.'%'];

        //姓名，昵称
        $name=input('name','','trim');
        if($name)$map[]=['name|nick','like','%'.$name.'%'];

        //按性别查询
        $sex=input('sex',-1);
        if($sex>-1)$map[]=['sex','=',$sex];

        //时间查询
        $time=input('time');
        $map=$this->condtime($map,$time);

        //排序方式
        $sort_type = input('sort_type','time_login');
        $sort_key = input('sort_key','desc');
        $sort = $sort_type.' '.$sort_key;
        $this->assign('sort_type',$sort_type);
        $this->assign('sort_key',$sort_key);

        $pagesize=10;
        $res  = db("user")->field('info',true)->where($map)->order($sort)->paginate($pagesize,false,['query'=>request()->param()]);
        $list = $res->toArray()['data'];

        $this->assign('sort_ary',$this->sort_ary);
        $this->assign('sort_atr',$this->sort_atr);

        //状态
        $reviewary=array(0=>'全部',1=>'启用',2=>'禁用');
        return view("",compact('res','list','reviewary','id','state','keyword','time','name','sex'));
    }


    //添加编辑配置参数
    public function edit(){
    	$id=input('id',0);

        if(request()->isPost()){
            $data=request()->post();
            $result = $this->validate($data ,'app\admin\validate\User');
            if($result !== true)$this->error($result);

            //注册时间
            if(!$id)$data['time_reg']=time();
            $this->editDataMethod($data);
        }

        if($id){
	        $map['id']=$id;
	        $info = db("user")->where($map)->find();
	        $this->assign('info', $info);
        }

        return view();
    }


    //会员资金明细
    //*+------------------------------------------------------------------------+*/

    //账号列表
    public function cash(){
        //按关键词查询
        $keyword=input('keyword','','trim');
        if($keyword)$map[] = ['info','like','%'.$keyword.'%'];

        //时间查询
        $map=$this->condtime($map);

        $pagesize=20;
        $sort="id desc";
        $res  = db("cash")->where($map)->order($sort)->paginate($pagesize,false,['query'=>request()->param()]);
        $list = $res->toArray()['data'];

        //获取会员信息
        if($list){
            $uid=parseArray($list,'uid');
            $umap[]=['id','in',$uid];
            $ulist=db('user')->where($umap)->column('id,account');
            $this->assign('urtn',$ulist);
        }

        //状态
        $reviewary=array(0=>'全部',1=>'启用',2=>'禁用');
        return view("",compact('res','list','reviewary','id','state','keyword','time','name','sex'));
    }

    //资金管理
    public function cashedit(){
        $uid=input('uid');
        if(!$uid)return '缺少会员账号';

        if(request()->isPost()){
            $cat=input('cat',0);
            if(!$cat)$this->error('请选择操作类型');

            $cash=input('cash');
            if(!is_numeric($cash) || $cash<0.01)$this->error('请正确输入金额');

            $info=input('info','','trim');
            //充值
            if(1==$cat){
               $data['cat']=1;
               $data['uid']=$uid;
               $data['cash']=$cash;
               $data['time']=time();
               $data['info']=$info;
               $data['aid']=UID;
               $cid=db('cash')->insertGetId($data);
               if(!$cid)return $this->error('充值失败');
               db('user')->where("id",$uid)->setInc('cash',$cash);
               return $this->success('充值成功');
            }

            //扣费
            $cash_left=db('user')->where("id",$uid)->value('cash');
            if(!$cash_left || $cash_left<$cash)$this->error('账户余额不足');

            $data['cat']=2;
            $data['uid']=$uid;
            $data['cash']=-$cash;
            $data['time']=time();
            $data['info']=$info;
            $data['aid']=UID;
            $cid=db('cash')->insertGetId($data);
            if(!$cid)return $this->error('扣费失败');
            db('user')->where("id",$uid)->setDec('cash',$cash);
            return $this->success('扣费成功');
        }

        $object=db('user');
        $map['id']=$uid;
        $info=$object->where($map)->find();

        return view('',compact('info'));
    }


    //会员余额明细
    public function cashuser(){
        $uid=input('uid');
        if(!$uid)return;
        $map['uid']=$uid;

        //按关键词查询
        $keyword=input('keyword','','trim');
        if($keyword)$map[] = ['info','like','%'.$keyword.'%'];

        //时间查询
        $map=$this->condtime($map);

        $pagesize=20;
        $sort="id desc";
        $res  = db("cash")->where($map)->order($sort)->paginate($pagesize,false,['query'=>request()->param()]);
        $list = $res->toArray()['data'];

        return view("",compact('res','list','uid','keyword'));
    }


}
